/*******************************************************************************
 * Copyright 2011 Alexandre Zglav and Morphiastic
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.morphiastic.analysis.filters;

import java.util.HashMap;
import java.util.Map;
import org.elasticsearch.common.settings.Settings;
import com.morphiastic.annotations.FilterDefinition;
import com.morphiastic.annotations.Parameter;


public class ElasticFilterFactory {

	
	public static ElasticFilter getFilterFromAnnotation(FilterDefinition filterDefAnnotation) {
		
		if(filterDefAnnotation == null ) return null;
		String defName = filterDefAnnotation.defName();
		HashMap<String,String> params = null;
		 
		if(filterDefAnnotation.parameters().length>0){
			params = new HashMap<String, String>();
			for (Parameter parameter : filterDefAnnotation.parameters()) {
				params.put(parameter.key(),parameter.value());
			}
		}
		
		ElasticFilterType type  = filterDefAnnotation.type();
		return buildFilter(defName, type, params);
	}
	
	 
	
	
	public static ElasticFilter buildFilter(String defName, ElasticFilterType type , Map<String,String> params){
		 
		if (defName.isEmpty()){
			if(params!=null){//we have custom params so this is a customized analyzer and we want to give it a propper name
				throw new IllegalArgumentException("No definition provided for custom ElasticFilter");
			}else{
				defName = type.toString();
			}
		}
		
		switch (type) {
			case asciifolding:
				return new ASCIIFoldingTokenFilter(defName);
			case dictionary_decompounder:
				return new DictionaryDecompounderTokenFilter(params, defName);
			case edgeNGram:
				return new EdgeNGramTokenFilter(params, defName);
			case elision:
				return new ElisionTokenFilter(params, defName);
			case html_strip:
				return new HTMLStripCharFilter(defName);
			case hyphenation_decompounder:
				return new HyphenationDecompounderTokenFilter(params, defName);
			case kstem:
				return new KStemTokenFilter(defName);
			case length:
				return new LengthTokenFilter(params, defName);
			case lowercase:
				return new LowercaseTokenFilter(defName);
			case nGram:
				return new NGramTokenFilter(params, defName);
			case phonetic:
				return new PhoneticTokenFilter(params, defName);
			case porterStem:
				return new PorterStemTokenFilter(defName);
			case reverse:
				return new ReverseTokenFilter(defName);
			case shingle:
				return new ShingleTokenFilter(params, defName);
			case snowball:
				return new SnowballTokenFilter(params, defName);
			case standard:
				return new StandardTokenFilter(defName);
			case stemmer:
				return new StemmerTokenFilter(params, defName);
			case stop:
				return new StopTokenFilter(params, defName);
			case synonym:
				return new SynonymTokenFilter(params, defName);
			case truncate:
				return new TruncateTokenFilter(params, defName);
			case unique:
				return new UniqueTokenFilter(params, defName);
			case word_delimiter:
				return new WordDelimiterTokenFilter(params, defName);
			default:
				return null;
		}
	}
	
	
	/**
	 * builds a filter from Settings obj
	 * @param name
	 * @param settings
	 * @return
	 */
	public static ElasticFilter getFilterFromSettings(String name, Settings settings){
		String typeVal = settings.get("type");
		ElasticFilterType type = ElasticFilterType.valueOf(typeVal);
		ElasticFilter filter = buildFilter(name, type, null);
		filter.setParamsFromSettings(settings);
		return filter;
	}


	
	
}
